import type { repUserInfo, repWxUserDtosInfo } from '@/services/login'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<repUserInfo>()

    // 保存会员信息，登录时使用
    const setProfile = (val: repUserInfo) => {
      profile.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    // 修改会员图片
    const updateProfileIcon = (imgUrl: string) => {
      profile.value!.userInfo.userIcon = imgUrl
    }
    // 修改会员信息
    const updateProfileInfo = (data: repWxUserDtosInfo) => {
      // profile.value!.userInfo.userIcon = imgUrl
      profile.value!.userInfo.userNickname = data.userNickname
      profile.value!.userDetailInfo.userBirthdayStr = data.userBirthdayStr
      profile.value!.userDetailInfo.userCity = data.userCity
      profile.value!.userDetailInfo.userCareer = data.userCareer
      profile.value!.userDetailInfo.userSex = data.userSex
    }

    // 记得 return
    return {
      profile,
      setProfile,
      clearProfile,
      updateProfileIcon,
      updateProfileInfo,
    }
  },
  // TODO: 持久化
  {
    // persist: true,
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
